package com.bojin.studio.ubattery;

import java.lang.reflect.Method;
import android.content.Context;
import android.util.Log;

public class TimeEstimation {
	private int mlevel;
	private double batteryCapacity=1500;
	private double radioOn=5;
	private double screenOn=200;
	private double wifiActive=250;
	private double radioActive=350;
	private double dspAudio=120;
	private double dspVideo=180;
	private double cpuIdle=5;
	private java.text.DecimalFormat df;
	private double factor=0;
	//private double cpuIdle;
	//private double cpuIdle;
	public TimeEstimation(Context context,int level)
	{
		mlevel=level;
		factor=level/100.0;
		df=new java.text.DecimalFormat("#.#");  
		//powCal=new PowerProfile(context);
		
		final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";
    	try{
        	final Class<?> PowerProfile_R = Class.forName(POWER_PROFILE_CLASS);
        	final Object mPowerProfile_ = PowerProfile_R.getConstructor(Context.class).newInstance(context);
        	
	    	Method getAveragePowerMeth = PowerProfile_R.getMethod("getAveragePower", String.class);
	    	Method getAveragePowerCPUMeth = PowerProfile_R.getMethod("getAveragePower", String.class, int.class);
	    	Method getNumSpeedStepsMeth = PowerProfile_R.getMethod("getNumSpeedSteps");
	    	
	        //POWER_NONE= (Double) getAveragePowerMeth.invoke(mPowerProfile_,Constants.POWER_NONE);
	        cpuIdle= (Double) getAveragePowerMeth.invoke(mPowerProfile_,Constants.POWER_CPU_IDLE);
	        //POWER_CPU_AWAKE= (Double) getAveragePowerMeth.invoke(mPowerProfile_,Constants.POWER_CPU_AWAKE);
	        //POWER_CPU_ACTIVE= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_CPU_ACTIVE);
	        //POWER_CPU_SPEEDS= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_CPU_SPEEDS);
	        //POWER_WIFI_SCAN= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_WIFI_SCAN);
	        //POWER_WIFI_ON= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_WIFI_ON);
	        wifiActive= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_WIFI_ACTIVE);
	        //POWER_GPS_ON= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_GPS_ON);
	        //POWER_BLUETOOTH_ON= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_BLUETOOTH_ON);
	        //POWER_BLUETOOTH_ACTIVE= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_BLUETOOTH_ACTIVE);
	        //POWER_BLUETOOTH_AT_CMD= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_BLUETOOTH_AT_CMD);
	        screenOn= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_SCREEN_ON);
	        //POWER_SCREEN_FULL= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_SCREEN_FULL);
	        radioOn= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_RADIO_ON);
	        //POWER_RADIO_SCANNING= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_RADIO_SCANNING);
	        radioActive= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_RADIO_ACTIVE);
	        dspAudio= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_AUDIO);
	        dspVideo= (Double) getAveragePowerMeth.invoke(mPowerProfile_, Constants.POWER_VIDEO);

	        batteryCapacity = (Double) getAveragePowerMeth.invoke(mPowerProfile_,Constants.POWER_BATTERY_CAPACITY);

		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
    	} catch (InstantiationException e) {
    		e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String get3gTalk()
	{
		Log.d("Paul","battery="+String.valueOf(batteryCapacity)+"dspAudio="+String.valueOf(dspAudio));
		return String.valueOf(df.format(factor*batteryCapacity/(dspAudio+radioActive)));
	}
	
	public String get2gTalk()
	{
		return String.valueOf(df.format(factor*batteryCapacity/(0.8*(dspAudio+radioActive))));
	}
	
	public String getInternetWifi()
	{
		return String.valueOf(df.format(factor*batteryCapacity/(screenOn+wifiActive)));
	}
	
	public String getInternetMobile()
	{
		return String.valueOf(df.format(factor*batteryCapacity/(screenOn+radioActive)));
	}
	
	public String getVideoPlay()
	{
		return String.valueOf(df.format(factor*batteryCapacity/(screenOn+dspAudio+dspVideo)));
	}
	
	public String getAudioPlay()
	{
		
		
		return String.valueOf(df.format(factor*batteryCapacity/(dspAudio*1.2)));
	}
	
	public String getStandby()
	{	    	
		return String.valueOf(df.format(factor*batteryCapacity/(radioOn+cpuIdle)));
	}
	
	private String timeTransfer(double input)
	{
	    double day=input/24;
		return "";
	}
	
	public static class Constants{
	    /**
	     * No power consumption, or accounted for elsewhere.
	     */
	    public static final String POWER_NONE = "none";
	    /**
	     * Power consumption when CPU is in power collapse mode.
	     */
	    public static final String POWER_CPU_IDLE = "cpu.idle";
	    /**
	     * Power consumption when CPU is awake (when a wake lock is held).  This
	     * should be 0 on devices that can go into full CPU power collapse even
	     * when a wake lock is held.  Otherwise, this is the power consumption in
	     * addition to POWERR_CPU_IDLE due to a wake lock being held but with no
	     * CPU activity.
	     */
	    public static final String POWER_CPU_AWAKE = "cpu.awake";
	    /**
	     * Power consumption when CPU is in power collapse mode.
	     */
	    public static final String POWER_CPU_ACTIVE = "cpu.active";
	    /**
	     * Power consumption when WiFi driver is scanning for networks.
	     */
	    public static final String POWER_WIFI_SCAN = "wifi.scan";
	    /**
	     * Power consumption when WiFi driver is on.
	     */
	    public static final String POWER_WIFI_ON = "wifi.on";
	    /**
	     * Power consumption when WiFi driver is transmitting/receiving.
	     */
	    public static final String POWER_WIFI_ACTIVE = "wifi.active";
	    /**
	     * Power consumption when GPS is on.
	     */
	    public static final String POWER_GPS_ON = "gps.on";
	    /**
	     * Power consumption when Bluetooth driver is on.
	     */
	    public static final String POWER_BLUETOOTH_ON = "bluetooth.on";
	    /**
	     * Power consumption when Bluetooth driver is transmitting/receiving.
	     */
	    public static final String POWER_BLUETOOTH_ACTIVE = "bluetooth.active";
	    /**
	     * Power consumption when Bluetooth driver gets an AT command.
	     */
	    public static final String POWER_BLUETOOTH_AT_CMD = "bluetooth.at";
	    /**
	     * Power consumption when screen is on, not including the backlight power.
	     */
	    public static final String POWER_SCREEN_ON = "screen.on";
	    /**
	     * Power consumption when cell radio is on but not on a call.
	     */
	    public static final String POWER_RADIO_ON = "radio.on";
	    /**
	     * Power consumption when cell radio is hunting for a signal.
	     */
	    public static final String POWER_RADIO_SCANNING = "radio.scanning";
	    /**
	     * Power consumption when talking on the phone.
	     */
	    public static final String POWER_RADIO_ACTIVE = "radio.active";
	    /**
	     * Power consumption at full backlight brightness. If the backlight is at
	     * 50% brightness, then this should be multiplied by 0.5
	     */
	    public static final String POWER_SCREEN_FULL = "screen.full";
	    /**
	     * Power consumed by the audio hardware when playing back audio content. This is in addition
	     * to the CPU power, probably due to a DSP and / or amplifier.
	     */
	    public static final String POWER_AUDIO = "dsp.audio";
	    /**
	     * Power consumed by any media hardware when playing back video content. This is in addition
	     * to the CPU power, probably due to a DSP.
	     */
	    public static final String POWER_VIDEO = "dsp.video";
	    public static final String POWER_CPU_SPEEDS = "cpu.speeds";
	    /**
	     * Battery capacity in milliAmpHour (mAh).
	     */
	    public static final String POWER_BATTERY_CAPACITY = "battery.capacity";
}

}
